<?php
class Controller_Widget extends Controller_Template
{
    public function __construct ($request, $response)
    {
        $this->template = 'smarty:blank';
        parent::__construct($request, $response);
    }

    public function action_vendor($uid)
    {
        if (site::type() != 'manager')
            return;

        $user = user::instance($uid);
        $this->template->set_filename('smarty:vendor/widget/info');
        $this->template->set('info', $user->get_vendor_info());
    }

    public function action_activities_from_channel ($channel_id)
    {
        $query = array(
            'channel' => (int) $channel_id,
            'include_activity_info' => TRUE,
            'include_channel_info'  => TRUE,
        );

        $result = Promotion::search($query, $offset = NULL, $limit = NULL);
        foreach ($result['list'] as & $rs)
        {
            if (substr($rs['url'], 0, 7) != 'http://')
                $rs['url'] = '';
        }

        $this->template->set_filename('smarty:channel/widget/activities');
        $this->template->set('activities', $result);
    }

    public function action_channels_from_activity ($activity_id)
    {
        $query = array(
            'activity' => (int) $activity_id,
            'include_channel_info'  => TRUE,
        );

        $result = Promotion::search($query, $offset = NULL, $limit = NULL);
        $this->template->set_filename('smarty:promotion/widget/channels');
        $this->template->set('channels', $result);
    }

    public function action_channel_filter ()
    {
        if (site::type() != 'manager')
            return ;

        $query      = $this->request->query();
        $request    = Request::$initial->query();
        $manager    = Arr::get($request, 'manager');
        $activity   = Arr::get($request, 'activity');

        $user_info = user::current_user()->get_user_info();
        if ($user_info['role_id'] == 'auditor')
                $manager = $user_info['uid'];
        else
        {
            $managers = Promotion_Activity::managers();
            array_unshift($managers, array(
                'uid'      => null,
                'realname' => '项目管理员',
            ));
            $this->template->set('managers',  $managers);
        }

        $activities = array();
        if ($manager > 0)
        {
            $result = Promotion_Activity::activity_by_manager($manager);
            foreach ($result as $act)
            {
                $activities[] = array(
                    'id'   => Arr::get($act, 'id'),
                    'name' => Arr::get($act, 'name'),
                );
            }
        }

        $vendors  = array();
        if ($activity > 0)
        {
            $result  = Promotion::vendors_by_activity ($activity);
            foreach ($result as $vendor)
            {
                $vendors[] = array(
                    'uid'   => Arr::get($vendor, 'uid'),
                    'name'  => Arr::get($vendor, 'name'),
                );
            }
        }

        array_unshift($activities, array(
                'id'   => null, 
                'name' => '活动列表',
            ));
        array_unshift($vendors, array(
                'uid'   => null, 
                'name' => '渠道商列表',
            ));


        $this->template->set_filename('smarty:channel/widget/filter');
		$this->template->set('activities', $activities);
		$this->template->set('vendors',    $vendors);
        $this->template->set('request',    $request);
    }

    /**
     * 推广效果过滤选择器
     *
     */
    public function action_effect_filter ()
    {
        $query      = $this->request->query();
        $request    = Request::$initial->query();
        $manager    = Arr::get($request, 'manager_id');
        $activity   = Arr::get($request, 'activity_id');

        if (site::type() == 'manager')
        {
            $user_info = user::current_user()->get_user_info();
            if ($user_info['role_id'] == 'auditor')
            {
                $manager = $user_info['uid'];
            }
            else if ($user_info['role_id'] == 'financial' && $user_info['passport'] == 'chengyufeng@appchina.com')
            {
                $manager = 217;
            }
        }

        if (! isset($request['start_date']) && isset($query['start_date']))
            $request['start_date'] = $query['start_date'];

        if (! isset($request['end_date']) && isset($query['end_date']))
            $request['end_date'] = $query['end_date'];

        $activities = array();
        if ($manager > 0)
        {
            $result = Promotion_Activity::activity_by_manager($manager, 'CPA');
            foreach ($result as $act)
            {
                $activities[] = array(
                    'id' => Arr::get($act, 'id'),
                    'name' => Arr::get($act, 'name'),
                );
            }
        }
        else if (site::type() == 'merchant')
        {
            $activities = Promotion::activities_by_merchant(user::current_user_id(), 'CPA');
        }

        $channels   = array();
        if ($activity > 0)
        {
            $result = Promotion::channels_by_activity ($activity);
            foreach ($result as $chn)
            {
                $channels[] = array(
                'unique_key'   => Arr::get($chn, 'unique_key'),
                'name'         => Arr::get($chn, 'name'),
                );
            }
        }

        array_unshift($activities, array(
                'id'   => 0,
                'name' => '全部活动',
            ));

        array_unshift($channels, array(
                'id'         => 0,
                'unique_key' => '',
                'name'       => '全部渠道',
            ));

        $this->template->set_filename('smarty:effect/widget/filter');
		$this->template->set('activities', $activities);
		$this->template->set('channels',   $channels);
        $this->template->set('managers',   Promotion_Activity::managers());
        $this->template->set('request',    $request);
    }

    /**
     * CPS效果过滤选择器
     *
     */
    public function action_cps_filter ()
    {
    	$query      = $this->request->query();
    	$request    = Request::$initial->query();
    	$manager    = Arr::get($request, 'manager_id');
    	$activity   = Arr::get($request, 'activity_id');
    
    	if (site::type() == 'manager')
    	{
    		$user_info = user::current_user()->get_user_info();
    		if ($user_info['role_id'] == 'auditor')
    			$manager = $user_info['uid'];
    	}
    
    	if (! isset($request['start_date']) && isset($query['start_date']))
    		$request['start_date'] = $query['start_date'];
    
    	if (! isset($request['end_date']) && isset($query['end_date']))
    		$request['end_date'] = $query['end_date'];
    
    	$activities = array();
    	if ($manager > 0)
    	{
    		$result = Promotion_Activity::activity_by_manager($manager);
    		foreach ($result as $act)
    		{
    			$activities[] = array(
    					'id' => Arr::get($act, 'id'),
    					'name' => Arr::get($act, 'name'),
    			);
    		}
    	}
    	else if (site::type() == 'merchant')
    	{
    		$activities = Promotion::activities_by_merchant(user::current_user_id());
    	}
    
    	$channels   = array();
    	if ($activity > 0)
    	{
    		$result = Promotion::channels_by_activity ($activity);
    		foreach ($result as $chn)
    		{
    			$channels[] = array(
    					'unique_key'   => Arr::get($chn, 'unique_key'),
    					'name'         => Arr::get($chn, 'name'),
    			);
    		}
    	}
    
    	array_unshift($activities, array(
    	'id'   => 0,
    	'name' => '全部活动',
    	));
    
    	array_unshift($channels, array(
    	'id'         => 0,
    	'unique_key' => '',
    	'name'       => '全部渠道',
    	));
    
    	$this->template->set_filename('smarty:effect/widget/filter');
    	$this->template->set('activities', $activities);
    	$this->template->set('channels',   $channels);
    	$this->template->set('managers',   Promotion_Activity::managers());
    	$this->template->set('request',    $request);
    }
    public function action_ajax_activity_list ($manager_uid)
    {
        $activities = Promotion_Activity::activity_by_manager($manager_uid);
        $result     = array();

        foreach ($activities as $act)
        {
            $result[] = array(
                'id' => Arr::get($act, 'id'),
                'name' => Arr::get($act, 'name'),
            );
        }
        return $this->json($result);
    }

    public function action_ajax_channel_list ($activity_id)
	{
		$channels = Promotion::channels_by_activity ($activity_id);
		$result   = array();
		$name = array();
		foreach ($channels as $chn)
		{
			$result[] = array(
					'key'   => Arr::get($chn, 'unique_key'),
					'name' => Arr::get($chn, 'name'),
					);
			$name[] = Arr::get($chn, 'name');
		}
		array_multisort($name, SORT_DESC,$result);
		return $this->json($result);
	}

    public function action_ajax_vendor_list ($activity_id)
    {
        $vendors  = Promotion::vendors_by_activity ($activity_id);
        $result   = array();

        foreach ($vendors as $vendor)
        {
            $result[] = array(
                'uid'   => Arr::get($vendor, 'uid'),
                'name'  => Arr::get($vendor, 'name'),
            );
        }

        return $this->json($result);
    }

    public function action_channel ($channel_id_or_key)
    {
        $this->template->set_filename("smarty:channel/widget/info");
        try {
	        if (is_integer($channel_id_or_key))
	        {
	            $chnid = (int) $channel_id_or_key;
	            $cinfo = Promotion_Channel::info($chnid);
	        }
	        else
	        {
	            $chnkey = trim(strtolower($channel_id_or_key));
	            $cinfo  = Promotion_Channel::info_key($chnkey);
	        }

            $this->template->set('channel', $cinfo);
        }
        catch (Exception $e)
        {
            $this->template->set_filename('smarty:blank');
        }
    }

    public function action_sdk_package ($pkgname)
    {
        $this->template->set_filename("smarty:SDK/widget/package");
        try {
	        if (is_integer($pkgname))
	        {
	            $chnid = (int) $pkgname;
	            $cinfo = SDK_Packages::info($chnid);
	        }
	        else
	        {
	            $chnkey = trim(strtolower($pkgname));
                $chnid  = SDK_Packages::package_id($chnkey);
	            $cinfo  = SDK_Packages::info($chnid);
	        }

            $developer_id   = Arr::get($cinfo, 'developer');
            $developer_info = SDK_Developers::info ($developer_id);

            $this->template->set('package', $cinfo);
            $this->template->set('developer', $developer_info);
        }
        catch (Exception $e)
        {
            $this->template->set_filename('smarty:blank');
        }
    }


	public static function sort_by_name($a,$b)
	{
		return $a['name'] > $b['name'];
	}

    private function json ($result)
    {
        echo json_encode($result);
        return;
    }
}
